/******************************************** 搜索JS ********************************************/

        /**
         * 选择搜索分类
         */
        function sel_cat(th) {
            $('#cat_val').val($.trim($(th).text()).toLowerCase());
            set_pla($.trim($(th).text()).toLowerCase())
        }

        /**
         * 修改标题
         */
        function set_pla(th) {
            switch (th) {
                case "ppt":
                    $("#so-kw").attr('placeholder','搜PPT')
                    break;
                case "word":
                    $("#so-kw").attr('placeholder','搜Word')
                    break;
                case "excel":
                    $("#so-kw").attr('placeholder','搜Excel')
                    break;
            }
        }

        var classifyHeadInp = $('.head-classify-inp');
        classifyHeadInp.focus(function(){
            if($('#xiala li').html() != undefined){
                $(this).siblings('.search-top').show();
            }
        });

        //绑定搜索按钮(按分类)
        $('#search-btn').on('click', formSearch1);
        function formSearch1(){
            var kw = $.trim($('#so-kw').val());
            var cat = $('#cat_val').val();
            var isSearch = $('#isSearch').val();
            var pageCat = isSearch ? '搜索页' : '综合页';
            var searchPosition = '';
            var c1 = 0;
            if(cat == 'ppt'){
                searchPosition = '企业办公'+pageCat+'PPT搜索';
                c1 = 3;
            }else if(cat == 'word'){
                searchPosition = '企业办公'+pageCat+'Word搜索';
                c1 = 9;
            }else if(cat == 'excel'){
                searchPosition = '企业办公'+pageCat+'Excel搜索';
                c1 = 8;
            }

            if(kw == '' || kw == '搜企业办公'){
                alert('请填写关键词');
                return false;
            }

        }


        $('#so-kw').keydown(function(event){if(event.which == 13){formSearch1();}});

        //各分类需要重写改方法
        function search1(kw){
            var catgory = $('#cat_val').val();
            if(!catgory){
                catgory = 'ppt'
            }
            $.get("/index.php?m=search&a=check", {kw:kw}, function(data) {
                if(data.status == 1){
                    tj_keyword()
                    setTimeout(function(){
                        if(data.rewrite){
                            window.location.href="/index.php?m=tags&a=index&class=0-0-0-0-0-1&key=" + data.py;
                        }else{
                            window.location.href=URL.DOMAIN+"/s-"+catgory+"/"+data.py+".html";
                        }
                    },500);
                }else{
                    alert('请填写有效的关键词');
                }
            }, 'json');
        }


        /**
         * 搜索
         */
        $("#so-kw").keyup(function(H){
            $('#history').remove();
            $('.recommend-wrap').css("padding-top","0");
            if (window.event) {
                var G = window.event.keyCode;
            } else {
                var G = H.which;
            }
            if (G != 38 && G != 40 && G != 13) {
                var num = 1;
                D = 0;
                if($('#so-kw').val() == ''){
                    //默认搜索词
                }else{
                    $('#sort').remove()
                    $.ajax({
                        type: 'POST',
                        url: '/index.php?m=Ajaxsearch&a=index',
                        data: {keyword: $('#so-kw').val()},
                        dataType: 'json',
                        success: function (rs) {
                            $('#xiala').html('');
                            var html = '';
                            for (var i in rs) {
                                html += '<li class="z_spanName">';
                                html +=    '<a id="u_'+num+'" onclick="on_search1('+ "'"+rs[i].keyword + "'" +',this)" href="javascript:;" class="every-record">';
                                html +=       '<span class="record-name">' + rs[i].keyword + '</span>';
                                html +=       '<span class="record-number-wrap"><span class="record-number">' + rs[i].icount + '</span>结果</span>';
                                html +=    '</a>';
                                html += '</li>';
                                $('#xiala').html(html);
                                num++
                            }
                            if(num <= 1){
                                $('.search-top').hide();
                            }else{
                                $('.search-top').show();
                            }
                        }
                    });
                }
            } else {
                topkey(H)
            }
        });

        /**
         * 联想搜索
         */
        var D = 0;
        function topkey(H) {
            if (window.event) {
                var G = window.event.keyCode;
            } else {
                var G = H.which;
            }
            if (G != 38 && G != 40 && G != 13) {
                D = 0;
            } else {
                if ($(".search-tips").eq(1).css("display") == "block") {
                    var I = $(".z_spanName").length;
                    if (G == 38) {
                        D--;
                        if (D < 1) {
                            D = I;
                            C(D);
                            F(1);
                        } else {
                            F(D + 1);
                            C(D);
                        }
                        return false;
                    }
                    if (G == 40) {
                        D++;
                        if (D > I) {
                            D = 1;
                            C(1);
                            F(I);
                        } else {
                            F(D - 1);
                            C(D);
                        }
                        return false;
                    }
                } else {
                    D = 0;
                }
            }
        }
        function C(G) {
            $("#u_" + G).parent().css("background-color", "#EBEBEB");
            var name = $("#u_" + G + " .record-name").html()
            $('#so-kw').val(name)
        }
        function F(G) {
            $("#u_" + G).parent().css("background-color", "#fff");
        }

        //模拟placeholder
        function placeholder($input){
            $input.val('').css('text-indent',0);
        }

        // 点击搜索框之外的地方隐藏搜索提示面板
        $(document).click(function(ev){
           /* console.log(ev.target)*/
            var current = $(ev.target);
            var hideClssifySearchTips = false;
            var hideFloatSearchTips = false;
            var hideHomeSearchBanner = false;
            var hideOfficeSearchBanner = false;
            // 最多查5层，判断是否是再搜索框内点击的
            for(var i = 0; i < 7; i++) {
                if(current.hasClass('search-input')) {
                    hideClssifySearchTips = true;
                    break;

                }else if (current.hasClass('float-search-input')){
                    hideFloatSearchTips = true;
                    break;
                }
                else if (current.hasClass('home-search-wrap')){
                    hideHomeSearchBanner = true;
                    break;
                }else if(current.hasClass('search-wrap')){
                    hideOfficeSearchBanner = true;
                    break;
                }
                else {
                    current =current.parent();
                }
            }
            if (!hideClssifySearchTips) {
                $('.pb-head-classify .search-tips').hide();
                /*classifyHeadInp.val('全站搜索').css('text-indent',8);*/
            }
            if (!hideFloatSearchTips) {
                $('.pb-float-head .float-search-tips').hide();
//            floadHeadInp.val('搜索素材').css('text-indent',10);
                floadHeadInp.css('text-indent',10);
            }
            if(!hideHomeSearchBanner) {
                $('.home .search-tips-bannner').hide();
                homeTopInp.val('搜索素材').css('text-indent',10);
            }
            if(!hideOfficeSearchBanner) {
                $('.classify-banner .search-tips').hide();
              /*  officeTopInp.val(plhd).css('text-indent',0);*/
            }
        });

        /******************************************** 搜索JS ********************************************/